package cn.com.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;
import cn.com.domain.content;



public class ViewPagerService {
//	xml 示例
/*	<?xml version="1.0" encoding="UTF-8"?>
	<contacts>
		<contact id="1">
			<image src="http://192.168.1.100:8080/TaskViewpager/images/a11.png"/>
		</contact>
		<contact id="2">
			<image src="http://192.168.1.100:8080/TaskViewpager/images/a12.png"/>
		</contact>	
		<contact id="3">
			<image src="http://192.168.1.100:8080/TaskViewpager/images/a13.png"/>
		</contact>		

	</contacts>*/
	
/*
 * 这里是测试用的，直接读取xml文件
 * 	public List<content> getContacts(String path) throws Exception {
//		URL url = new URL(path);
//		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//		conn.setConnectTimeout(5000);
//		conn.setRequestMethod("GET");
//		if (conn.getResponseCode() == 200 || true) {
//			FileInputStream fis = new FileInputStream(new File("file:///android_asset/list.xml"));
////			InputStream is = conn.getInputStream(); // 得到服务端输入流获取到list.xml,contact信息解析
//			return parserXMLContact(fis);  //xml 解析
//		}
//		return null;
		
		InputStream is = getClass().getResourceAsStream("/assets/list.xml");
		return parserXMLContact(is);  //xml 解析
	}*/
	
	public List<content> getContacts(String path)  {
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			if (conn.getResponseCode() == 200) {
				InputStream is = conn.getInputStream(); // 得到服务端输入流获取到list.xml,contact信息解析
				return parserforXMLContact(is);  //xml 解析
			}
		}catch (Exception e) {
			e.printStackTrace();
			Log.i("Other", "soga");
		}
//		return null;
		return new ArrayList<content>();
		
		
	}
	
	public List<content> parserforXMLContact(InputStream is) throws Exception {
		
		 List<content> contacts = null ;
		 content contact = null ;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(is, "UTF-8"); // 解析哪个流
		// 解析的事件类型
		int eventType = parser.getEventType();

		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_TAG:
				if("contacts".equals(parser.getName())){
					contacts = new ArrayList<content>();
				}else if("contact".equals(parser.getName())){
					contact = new content();
					int id = Integer.valueOf(parser.getAttributeValue(0));
					contact.setId(id);
				}else if("image".equals(parser.getName())){
					String src = parser.getAttributeValue(0);
					contact.setImgsrc(src);
				}
				break;
			case XmlPullParser.END_TAG:
				if("contact".equals(parser.getName())){
					contacts.add(contact);
					contact = null ;
				}
				break ;

			default:
				break;
			}

			eventType = parser.next();
		}
		is.close() ; 
		return contacts;
	}

}
